package com.geodroid.common.kernel.algos;






import com.geodroid.common.kernel.geos.GeoElement;
import com.geodroid.common.kernel.geos.GeoPoint2;

public class AlgoMidPoint extends AlgoElement {

	private GeoPoint2 M; // output     
    private GeoPoint2 P, Q; // input
    
    
	
	
	public AlgoMidPoint(GeoPoint2 P, GeoPoint2 Q) {
        //super(cons);
        this.P = P;
        this.Q = Q;
        // create new Point
        M = new GeoPoint2();
        setInputOutput();

        // compute M = (P + Q)/2
        compute(); 
	}

	

	@Override
	protected void compute() {
        boolean pInf = P.isInfinite();
        boolean qInf = Q.isInfinite();
        

        if (!pInf && !qInf) {
            // M = (P + Q) / 2          
            computeMidCoords();
        } else if (pInf && qInf){
          //  M.setUndefined();
        }
        else if (pInf)
            copyCoords(P);
        else // qInf
        	copyCoords(Q);
		
	}






	private void computeMidCoords() {
		GeoPoint2 P = (GeoPoint2) getP();
        GeoPoint2 Q = (GeoPoint2) getQ();
        
    	getPoint().setCoords(
                (P.inhomX + Q.inhomX) / 2.0d,
                (P.inhomY + Q.inhomY) / 2.0d,
                1.0);
		
	}
	
	protected void copyCoords(GeoPoint2 point){
    	getPoint().setCoords((GeoPoint2) point);
    }


	/**
     * 
     * @return first input point
     */
    protected GeoPoint2 getP() {
        return P;
    }
    
    public GeoPoint2 getPoint() {
		// TODO Auto-generated method stub
		return this.M;
	}
    
	/**
     * 
     * @return second input point
     */
    protected GeoPoint2 getQ() {
        return Q;
    }
	
	// for AlgoElement
    @Override
	protected void setInputOutput() {
        input = new GeoElement[2];
        input[0] = (GeoElement) P;
        input[1] = (GeoElement) Q;
        GeoElement [] out = new GeoElement[1] ;
        out[0] = M ; 
        setOutput(out) ; 	
   //     setOnlyOutput(M);
        setDependencies(); // done by AlgoElement
    }

}
